<?php
namespace Home\Model;
use Think\Model\RelationModel;

class QuestionModel extends RelationModel{
    //关联学生答题表
    protected $_link = array(
        //用于联级删除的,当删除一个题目的时候同时把对应的答题数据删除掉!
        "delete_question"=>array(
            "mapping_type"=>3,
            "class_name"=>"student_question",
            "mapping_name"=>"delete_question",
            "foreign_key"=>"que_id",
            "parent_key"=>"que_id",
            "mapping_fields"=>"que_id",         //只取出外键,删除就删除,不需要提取过多的数据,省流量
        ),

    );
    //自动验证
    protected $_validate = array(
        //修改题目的时候进行验证
        array("que_id","number","题目id必须是正整数!",1,"regex",2),
        //新增题目的时候进行验证
        array("que_type",array(0,1,2,3,4),"题目类型只有判断,单选,多选,填空,问答!",1,"in",1),
        array("que_content","require","题目内容不能为空!",1,"regex",3),
        array("que_num","number","题目题号必须是正整数!",1,"regex",1),
        array("que_answer","require","题目答案不能为空!",1,"regex",3),
        array("que_score","number","题目分数范围在1-100的正整数!",1,"regex",3),
        array("que_score","checkScore","题目分数范围在1-100的正整数!",1,"callback",3),
        array("qus_id","number","所属试卷id必须是正整数!",1,"regex",1),
    );

    protected function checkScore($que_score){
        if($que_score<=0 || $que_score >100){
            return false;
        }
        return true;
    }
}